#include "iConfig.hpp"

#include <glf/iphone/EAGLView.h>

#include <glf/config.h>
#include <glf/app.h>
#include <glf/input/inputDevice.h>
#include <glf/input/inputEventType.h>

@implementation MainGLView

- (id) initWithFrame:(CGRect)frame {    
    if ((self = [super initWithFrame:frame])) {
		[self setMultipleTouchEnabled:YES];
		[self setExclusiveTouch:YES];
	}	
    return self;
}

//UIResponder protocol
- (BOOL)canBecomeFirstResponder {
	return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	for( UITouch* touch in touches) {
		CGPoint cursor = [touch locationInView:self];
		GetAppInputMgr().GetGamepad(0).RaiseTouchEvent(0, glf::IET_TOUCHPAD_BEGAN, (uint)touch, cursor.x, cursor.y);
	}	
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	for( UITouch* touch in touches) {
		CGPoint cursor = [touch locationInView:self];
		GetAppInputMgr().GetGamepad(0).RaiseTouchEvent(0, glf::IET_TOUCHPAD_MOVED, (uint)touch, cursor.x, cursor.y);
	}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	for( UITouch* touch in touches) {
		CGPoint cursor = [touch locationInView:self];
		GetAppInputMgr().GetGamepad(0).RaiseTouchEvent(0, glf::IET_TOUCHPAD_ENDED, (uint)touch, cursor.x, cursor.y);
	}
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	[self touchesEnded:touches withEvent:event];
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	if(motion == UIEventSubtypeMotionShake)
	{
//		glf::IphoneInput motionGLF = glf::IphoneInput(glf::EVENT_IPHONE_SHAKE_BEGAN, event.timestamp, 0, 0, 0);
//		[self PushInput:motionGLF];
	}
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	if(motion == UIEventSubtypeMotionShake)
	{
//		glf::IphoneInput motionGLF = glf::IphoneInput(glf::EVENT_IPHONE_SHAKE_ENDED, event.timestamp, 0, 0, 0);
//		[self PushInput:motionGLF];
	}
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
	[self motionEnded:motion withEvent:event];
/*
	if(motion == UIEventSubtypeMotionShake)
	{
//		glf::IphoneInput motionGLF = glf::IphoneInput(glf::EVENT_IPHONE_SHAKE_ENDED, event.timestamp, 0, 0, 0);
//		[self PushInput:motionGLF];
	}
*/
 }

@end
